package com.yxzr.youyou.fragment.vip;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.R;
import com.yxzr.youyou.adapter.ClassificationAdapter;
import com.yxzr.youyou.adapter.MovieOneRecyclerViewAdapter;
import com.yxzr.youyou.base.BaseFragment;
import com.yxzr.youyou.bean.MovieBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class VipTVPlayFragment extends BaseFragment {
    private RecyclerView movieRV;
    private LinearLayout movie_linear;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_tvplay,null);
        initView(v);
        return v;
    }
    private void initView(View v) {
        movieRV = v.findViewById(R.id.movie_rv);
        movie_linear = v.findViewById(R.id.movie_linear);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),4);
        movieRV.setLayoutManager(gridLayoutManager);
        movieRV.addItemDecoration(new RecyclerItemDecoration(2,4));


        initData();
    }

    private void initData(){
        RequestUtil.getInstence().API().movie("1","2")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        MovieBean bean = gson.fromJson(t,MovieBean.class);
                        if (bean.getData() != null && bean.getData().size() > 0){
                            movieRV.setAdapter(new ClassificationAdapter(getContext(),bean.getData(),"2",1));
                        }

                        for (int i = 0;i < bean.getData().size();i++){
                            View cView = LayoutInflater.from(getContext()).inflate(R.layout.home_frame_layout,null);
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            movie_linear.addView(cView,lp);

                            TextView textView = cView.findViewById(R.id.home_frame_text);
                            RecyclerView recyclerView = cView.findViewById(R.id.moviehome_frame_rv);
                            textView.setText(bean.getData().get(i).getTitle());

                            GridLayoutManager layoutManager = new GridLayoutManager(getContext(),3);
                            recyclerView.setLayoutManager(layoutManager);
                            recyclerView.addItemDecoration(new RecyclerItemDecoration(10,3));
                            recyclerView.setNestedScrollingEnabled(false);
                            if (bean.getData().get(i).getData() != null && bean.getData().get(i).getData().size() > 0){
                                recyclerView.setAdapter(new MovieOneRecyclerViewAdapter(getContext(),bean.getData().get(i).getData(),1));
                            }
                        }

                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
